package com.cis.education.service.impl;

import java.util.List;

import com.cis.education.domain.EduCheckinStudent;
import com.cis.education.domain.EduCourseStudent;
import com.cis.education.mapper.EduCheckinStudentMapper;
import com.cis.education.mapper.EduCourseStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cis.education.mapper.EduCheckinMapper;
import com.cis.education.domain.EduCheckin;
import com.cis.education.service.IEduCheckinService;
import com.cis.common.core.text.Convert;

/**
 * 签到管理Service业务层处理
 * 
 * @author 管理员
 * @date 2022-01-19
 */
@Service
public class EduCheckinServiceImpl implements IEduCheckinService 
{
    @Autowired
    private EduCheckinMapper eduCheckinMapper;

    @Autowired
    private EduCourseStudentMapper eduCourseStudentMapper;

    @Autowired
    private EduCheckinStudentMapper eduCheckinStudentMapper;

    /**
     * 查询签到管理
     * 
     * @param id 签到管理主键
     * @return 签到管理
     */
    @Override
    public EduCheckin selectEduCheckinById(Integer id)
    {
        return eduCheckinMapper.selectEduCheckinById(id);
    }

    /**
     * 查询签到管理列表
     * 
     * @param eduCheckin 签到管理
     * @return 签到管理
     */
    @Override
    public List<EduCheckin> selectEduCheckinList(EduCheckin eduCheckin)
    {
        return eduCheckinMapper.selectEduCheckinList(eduCheckin);
    }

    /**
     * 新增签到管理
     * 
     * @param eduCheckin 签到管理
     * @return 结果
     */
    @Override
    public int insertEduCheckin(EduCheckin eduCheckin)
    {
        //新增签到成功后将签到学生添加到签到学生关系表
        int row = eduCheckinMapper.insertEduCheckin(eduCheckin);
        if (row != 0){
            Integer checkinId = eduCheckin.getId();
            Integer courseId = eduCheckin.getCourseId();
            //查询选择当前课程的学生
            EduCourseStudent eduCourseStudent = new EduCourseStudent();
            eduCourseStudent.setCourseId(courseId);
            List<EduCourseStudent> eduCourseStudents = eduCourseStudentMapper.selectEduCourseStudentList(eduCourseStudent);
            for (int i = 0; i < eduCourseStudents.size(); i++) {
                EduCheckinStudent eduCheckinStudent = new EduCheckinStudent();
                eduCheckinStudent.setCheckinId(checkinId);
                eduCheckinStudent.setStudentId(eduCourseStudents.get(i).getStudentId());
                eduCheckinStudent.setStatus(0);
                eduCheckinStudentMapper.insertEduCheckinStudent(eduCheckinStudent);
            }
        }

        return row;
    }

    /**
     * 修改签到管理
     * 
     * @param eduCheckin 签到管理
     * @return 结果
     */
    @Override
    public int updateEduCheckin(EduCheckin eduCheckin)
    {
        return eduCheckinMapper.updateEduCheckin(eduCheckin);
    }

    /**
     * 批量删除签到管理
     * 
     * @param ids 需要删除的签到管理主键
     * @return 结果
     */
    @Override
    public int deleteEduCheckinByIds(String ids)
    {
        return eduCheckinMapper.deleteEduCheckinByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除签到管理信息
     * 
     * @param id 签到管理主键
     * @return 结果
     */
    @Override
    public int deleteEduCheckinById(Integer id)
    {
        return eduCheckinMapper.deleteEduCheckinById(id);
    }
}
